package com.lan.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lan.entity.sys.User;
import com.lan.enums.sys.user.ResultEnum;
import com.lan.mapper.sys.UserMapper;
import com.lan.service.sys.UserService;
import com.lan.viewObject.Result;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  用户 ServiceImpl
 * </p>
 *
 * @author shadow
 * @since 2022-06-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User getById(String id) {
        return null;
    }

    @Override
    public List<User> getListByWrapper(final User user) {
        return getList(user, new QueryWrapper<>());
    }

    @Override
    public List<User> getListBySQL(final String type, final User user) {
        return userMapper.getListBySQL(type, user);
    }

    @Override
    public User getUserByAccount(final String account) {
        User user = new User();
        user.setAccount(account);
        List<User> userList = getListByWrapper(user);
        if (CollectionUtils.isNotEmpty(userList))
            return userList.get(0);
        return null;
    }


    public User getUserById(final String id) {
        if (null != id && id.length() > 0) {
            User user = new User();
            user.setId(id);
            List<User> userList = getListByWrapper(user);
            if (CollectionUtils.isNotEmpty(userList))
                return userList.get(0);
        }
        return null;
    }

    @Override
    public Integer getCountByWrapper(final User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        Optional.ofNullable(user.getSex())
                .filter(StringUtils::isNotBlank)
                .ifPresent(x -> wrapper.eq("sex", x));
        return userMapper.selectCount(wrapper);
    }

    @Override
    public ResultEnum deleteByStatus(String id) {
        User user = userMapper.selectById(id);
        if (null == user)
//            return Result.error(0, "用户不存在");
        user.setStatus("0");
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", "0");
        updateWrapper.eq("id", id);
        userMapper.update(user, updateWrapper);
        return null;
    }

    @Override
    public int deleteById(final String id) {
        return userMapper.deleteById(id);
    }

    private List<User> getList(final User user, QueryWrapper<User> wrapper) {
        Optional.ofNullable(user.getId())
                .filter(StringUtils::isNotBlank)
                .ifPresent(x -> wrapper.eq("id", x));
        Optional.ofNullable(user.getName())
                .filter(StringUtils::isNotBlank)
                .ifPresent(x -> wrapper.eq("name", x));
        Optional.ofNullable(user.getAccount())
                .filter(StringUtils::isNotBlank)
                .ifPresent(x -> wrapper.eq("account", x));
        wrapper.eq("status", "1");
        wrapper.orderByDesc("created");
        return userMapper.selectList(wrapper);
    }
}
